package com.HRs.Dao;

import com.HRs.domain.*;
import com.HRs.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class PatientDaolmpl implements PatientDao{

    @Override
    public boolean RegisterFor_p(Patient p) {
        Connection con = null;
        Statement st = null;

        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();

            String sql = "insert into patient_info(p_Pid, p_Name, p_Password, p_Sex, p_PhoneNumber, p_Address, p_ID) VALUE\n" +
                    "('"+p.getP_Pid()+"','"+p.getP_Name()+"','"+p.getP_Password()+"','"+p.getP_Sex()+"','"+p.getP_PhoneNumber()+
                    "','"+p.getP_Address()+"','"+p.getP_ID()+"');";

            st.executeUpdate(sql);

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            JDBCUtils.close(con, st);
        }
        return true;
    }

    @Override
    public void Order_p(patientorder_info pi) {
        Connection con = null;
        Statement st = null;

        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();

            String sql = "insert into patientorder_info(po_Pid, po_Department, po_Doctor, po_Date, po_Id) VALUE ('"+pi.getPo_Pid()+"','"+pi.getPo_Department()+"','"+pi.getPo_Doctor()+"','"+pi.getPo_Date()+"','"+pi.getPo_Id()+"')";

            st.executeUpdate(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st);
        }
    }

    @Override
    public boolean Login_p(String Id, String Password) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select p_Password from patient_info where p_Pid = '"+Id+"'";
            rs = st.executeQuery(sql);
            String password = "";
            while(rs.next()){
                password = rs.getString("p_Password");
            }
            if(password.equals("")){
                return false;
            }else return password.equals(Password);

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    @Override
    public ArrayList<Patient> get_pat() {
        ArrayList<Patient> pat = new ArrayList<Patient>();

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select * from patient_info";
            rs = st.executeQuery(sql);
            String p_Pid = "";
            String p_Name = "";
            String p_Password = "";
            String p_Sex = "";
            String p_PhoneNumber = "";
            String p_Address = "";
            String p_ID = "";

            while(rs.next()){
                p_Pid = rs.getString("p_Pid");
                p_Name = rs.getString("p_Name");
                p_Password = rs.getString("p_Password");
                p_Sex = rs.getString("p_Sex");
                p_PhoneNumber = rs.getString("p_PhoneNumber");
                p_Address = rs.getString("p_Address");
                p_ID = rs.getString("p_ID");
                pat.add(new Patient(p_Pid, p_Name, p_Password, p_Sex, p_PhoneNumber, p_Address,p_ID));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }

        return pat;
    }

    @Override
    public void delete_row(int row) {
        Integer r = row;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "call deleteP_row(" +  r.toString() + ")";
            rs = st.executeQuery(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    @Override
    public void insert(Patient p) {
        Connection con = null;
        Statement st = null;

        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();

            String sql = "insert into patient_info(p_Pid, p_Name, p_Password, p_Sex, p_PhoneNumber, p_Address, p_ID) VALUE\n" +
                    "('"+p.getP_Pid()+"','"+p.getP_Name()+"','"+p.getP_Password()+"','"+p.getP_Sex()+"','"+p.getP_PhoneNumber()+
                    "','"+p.getP_Address()+"','"+p.getP_ID()+"');";

            st.executeUpdate(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st);
        }
    }

    @Override
    public ArrayList<patientorder_info> Search_hisInfo(String id) {
        ArrayList<patientorder_info> list_Po = new ArrayList<patientorder_info>();

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select * from patientorder_info where po_Pid = '"+id+"'";
            rs = st.executeQuery(sql);
            String po_Pid = "";
            String po_Department = "";
            String po_Doctor = "";
            String po_Date = "";
            String po_Id = "";

            while(rs.next()){
                po_Pid = rs.getString("po_Pid");
                po_Department = rs.getString("po_Department");
                po_Doctor = rs.getString("po_Doctor");
                po_Date = rs.getString("po_Date");
                po_Id = rs.getString("po_Id");
                list_Po.add(new patientorder_info(po_Pid, po_Department, po_Doctor, po_Date, po_Id));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }

        return list_Po;
    }

    @Override
    public ArrayList<DoctorOrdered> Search_OrderInfo(String dp) {
        ArrayList<DoctorOrdered> list_DO = new ArrayList<>();

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select * from doctorordered_info\n" +
                    "where do_Department = '"+dp+"'";
            System.out.println("科室:" + dp);
            rs = st.executeQuery(sql);
            String do_Id = "";
            String do_Department = "";
            String do_Name = "";
            int do_Num = 0;
            String do_flag = "";

            while(rs.next()){
                do_Id = rs.getString("do_Id");
                do_Department = rs.getString("do_Department");
                do_Name = rs.getString("do_Name");
                do_Num = rs.getInt("do_Num");
                do_flag = rs.getString("do_flag");
                list_DO.add(new DoctorOrdered(do_Id,do_Department, do_Name,do_Num,do_flag));
                System.out.println(new DoctorOrdered(do_Id,do_Department, do_Name,do_Num,do_flag));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }

        return list_DO;
    }

    @Override
    public ArrayList<DoctorOrdered> Search_OrderInfo_Zd(String dp, String dt) {
        ArrayList<DoctorOrdered> list_DO = new ArrayList<>();

        Connection con = null;
        Statement st = null;
        ResultSet rs2 = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql3 = "call flush_SetZero()";
            String sql = "call flush_DocOdInfo_Zd('"+dt+"')";
            String sql2 = "select *\n" +
                    "from doctorordered_info where do_Department = '"+dp+"'";
            System.out.println("科室:" + dp);
            st.execute(sql3);
            st.execute(sql);
            rs2 = st.executeQuery(sql2);
            String do_Id = "";
            String do_Department = "";
            String do_Name = "";
            int do_Num = 0;
            String do_flag = "";

            while(rs2.next()){
                do_Id = rs2.getString("do_Id");
                do_Department = rs2.getString("do_Department");
                do_Name = rs2.getString("do_Name");
                do_Num = rs2.getInt("do_Num");
                do_flag = rs2.getString("do_flag");
                list_DO.add(new DoctorOrdered(do_Id,do_Department, do_Name,do_Num,do_flag));
                System.out.println(new DoctorOrdered(do_Id,do_Department, do_Name,do_Num,do_flag));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs2);
        }

        return list_DO;
    }

    @Override
    public void flush_DocInfo() {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "call flush_DocOdInfo()";
            st.execute(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    @Override
    public void flush_SetZero_DocInfo() {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "call flush_SetZero()";
            st.execute(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    @Override
    public String get_Pid() {
        String pid = null;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select * from patient_info";
            rs = st.executeQuery(sql);
            String p_Pid = null;

            while(rs.next()){
                p_Pid = rs.getString("p_Pid");
//                System.out.println("p_Pid:" + p_Pid);
            }
//            assert p_Pid != null;
//            System.out.println("0-7:" + p_Pid.substring(0, 8));
            if(p_Pid == null){
                pid = sdf.format(d) + "1001";
            }else if (p_Pid.substring(0, 8).equals(sdf.format(d))){
                Integer tmp = 1000;
                while(!tmp.toString().equals(p_Pid.substring(8,12))){
                    tmp ++ ;
                }
                tmp ++;
//                System.out.println("tmp:" + tmp);
                pid = sdf.format(d) + tmp.toString();

            }else {
                pid = sdf.format(d) + "1001";
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
        return pid;
    }

    @Override
    public ArrayList<String> get_DocName(String dp){
        ArrayList<String> list_Dn = new ArrayList<>();


        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select doc_Name\n" +
                    "from doctor_info where doc_Department = '"+dp+"'";
            rs = st.executeQuery(sql);
            String doc_Name = "";

            while(rs.next()){
                doc_Name = rs.getString("doc_Name");

                list_Dn.add(new String(doc_Name));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
        return list_Dn;
    }

    public static void main(String[] args) {
        PatientDaolmpl pd = new PatientDaolmpl();
        System.out.println(pd.get_Pid());
    }
}
